package com.orangelife.mobile.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.constants.ParkingCardURL;
import com.orangelife.mobile.main.fragment.GoodsRentOrderFragment;
import com.orangelife.mobile.main.fragment.ParkingCardFragment;
import com.orangelife.mobile.main.fragment.RepairFragment;
import com.orangelife.mobile.util.BLog;
import com.orangelife.mobile.washcar.fragment.WashCarOrderFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 服务订单
 * Created by yw on 2015/11/25.
 */
public class ServiceOrderActivity extends OrangeLifeBaseActivity implements View.OnClickListener {


    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.tvFirst)
    TextView tvFirst;
    @Bind(R.id.viewFirst)
    View viewFirst;
    @Bind(R.id.ll_submit_form)
    RelativeLayout llSubmitForm;
    @Bind(R.id.tvSecond)
    TextView tvSecond;
    @Bind(R.id.viewSecond)
    View viewSecond;
    @Bind(R.id.ll_history_record)
    RelativeLayout llHistoryRecord;
    @Bind(R.id.flContent)
    FrameLayout flContent;
    @Bind(R.id.tvThird)
    TextView tvThird;
    @Bind(R.id.viewThird)
    View viewThird;
    @Bind(R.id.llThird)
    RelativeLayout llThird;
    @Bind(R.id.tvFourth)
    TextView tvFourth;
    @Bind(R.id.viewFourth)
    View viewFourth;
    @Bind(R.id.llFourth)
    RelativeLayout llFourth;


    private int flags = 1;
    /**
     * 小区公告列表接口地址
     */
    public static final String URL_REPAIR_LIST_NEW = Constant.URL_REPAIR_LIST_NEW;
    /**
     * 系统消息列表接口地址
     */
//    public static final String URL_PARKCARD_PAY_MINE = Constant.URL_PARKCARD_PAY_MINE;
    public static final String URL_PARKCARD_PAY_MINE = ParkingCardURL.PARKCARD_PAY_MINE;
    private int type;

    public FragmentManager fm;
    public FragmentTransaction ft;

    private Fragment repairFrament;
    private Fragment parkingFrament;
    private Fragment goodsRentFragment;
    private Fragment washCarFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_order);
        ButterKnife.bind(this);
        findView();
        getLocation();
    }

    /**
     * 初始化跳转到租借订单列表
     */
    private void getLocation() {
        if (getIntent().getIntExtra("location", 1) == 3) {
            jumpGoodsRentFragment();
        }else if(getIntent().getIntExtra("location",1)==4){
            jumpWashCarFragmemnt();
        }
    }

    /**
     * 赋值
     */
    private void findView() {
        Intent intent = getIntent();
        type = intent.getIntExtra("type", 1);
        setFirtColor();
        tvTitle.setText("服务订单");
        ivTitleRight.setVisibility(View.GONE);
        tvFirst.setText("房屋维修");
        tvSecond.setText("公共停车");
        tvThird.setText("便民租借");
        tvFourth.setText("上门洗车");
        llHistoryRecord.setOnClickListener(this);
        llSubmitForm.setOnClickListener(this);
        llThird.setOnClickListener(this);
        llFourth.setOnClickListener(this);


        repairFrament = new RepairFragment();
        parkingFrament = new ParkingCardFragment();
        goodsRentFragment = new GoodsRentOrderFragment();
        washCarFragment = new WashCarOrderFragment();
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        ft.add(R.id.flContent, repairFrament);
        ft.commit();
    }

    private void changeFragment() {

    }

    private void setFirtColor() {

        tvFirst.setTextColor(getResources().getColor(R.color.lightOrange));
        viewFirst.setVisibility(View.VISIBLE);
        tvSecond.setTextColor(getResources().getColor(R.color.DarkGray));
        viewSecond.setVisibility(View.GONE);
        tvThird.setTextColor(getResources().getColor(R.color.DarkGray));
        viewThird.setVisibility(View.GONE);
        tvFourth.setTextColor(getResources().getColor(R.color.DarkGray));
        viewFourth.setVisibility(View.GONE);
    }

    private void setTvSecondColor() {
        tvSecond.setTextColor(getResources().getColor(R.color.lightOrange));
        viewSecond.setVisibility(View.VISIBLE);
        tvFirst.setTextColor(getResources().getColor(R.color.DarkGray));
        viewFirst.setVisibility(View.GONE);
        tvThird.setTextColor(getResources().getColor(R.color.DarkGray));
        viewThird.setVisibility(View.GONE);
        tvFourth.setTextColor(getResources().getColor(R.color.DarkGray));
        viewFourth.setVisibility(View.GONE);
    }

    private void setTvThirdColor() {
        tvThird.setTextColor(getResources().getColor(R.color.lightOrange));
        viewThird.setVisibility(View.VISIBLE);
        tvFirst.setTextColor(getResources().getColor(R.color.DarkGray));
        viewFirst.setVisibility(View.GONE);
        tvSecond.setTextColor(getResources().getColor(R.color.DarkGray));
        viewSecond.setVisibility(View.GONE);
        tvFourth.setTextColor(getResources().getColor(R.color.DarkGray));
        viewFourth.setVisibility(View.GONE);
    }
    private void setTvFourthColor() {
        tvFourth.setTextColor(getResources().getColor(R.color.lightOrange));
        viewFourth.setVisibility(View.VISIBLE);
        tvThird.setTextColor(getResources().getColor(R.color.DarkGray));
        viewThird.setVisibility(View.GONE);
        tvFirst.setTextColor(getResources().getColor(R.color.DarkGray));
        viewFirst.setVisibility(View.GONE);
        tvSecond.setTextColor(getResources().getColor(R.color.DarkGray));
        viewSecond.setVisibility(View.GONE);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_history_record:
                setTvSecondColor();
                type = 1;
                ft = fm.beginTransaction();
                if (parkingFrament.isAdded())
                    ft.show(parkingFrament);
                else
                    ft.add(R.id.flContent, parkingFrament);
                ft.hide(repairFrament);
                ft.hide(goodsRentFragment);
                ft.hide(washCarFragment);
                ft.commit();
                break;

            case R.id.ll_submit_form:
                setFirtColor();
                type = 2;
                ft = fm.beginTransaction();
                ft.show(repairFrament);
                ft.hide(parkingFrament);
                ft.hide(goodsRentFragment);
                ft.hide(washCarFragment);
                ft.commit();
                break;

            case R.id.llThird:
                jumpGoodsRentFragment();
                break;
            case R.id.llFourth:
                jumpWashCarFragmemnt();
                break;

        }
    }

    private void jumpGoodsRentFragment() {
        setTvThirdColor();
        type = 3;
        ft = fm.beginTransaction();
        if (goodsRentFragment.isAdded())
            ft.show(goodsRentFragment);
        else
            ft.add(R.id.flContent, goodsRentFragment);
        ft.hide(repairFrament);
        ft.hide(parkingFrament);
        ft.hide(washCarFragment);
        ft.commit();
    }

    private void jumpWashCarFragmemnt(){
        setTvFourthColor();
        type = 4;
        ft = fm.beginTransaction();
        if (washCarFragment.isAdded())
            ft.show(washCarFragment);
        else
            ft.add(R.id.flContent, washCarFragment);
        ft.hide(repairFrament);
        ft.hide(parkingFrament);
        ft.hide(goodsRentFragment);
        ft.commit();
    }
    @Override
    protected void onResume() {
        super.onResume();
        BLog.getInstance().intAppmod(BLog.SERVICEORDER);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BLog.getInstance().outAppmod(BLog.SERVICEORDER);
    }
}

